package service.impl;

import com.github.pagehelper.PageHelper;
import mapper.FilmMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import po.Film;
import po.filmContext;
import poi.WriteExcel;
import service.FilmService;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class FilmServeiceImpl implements FilmService {
    @Autowired
    FilmMapper filmMapper;
    @Override
    public List<Film> getFilms() {
        List<Film> films =filmMapper.getFilms();
        return films;
    }

    @Override
    public String getFilmIntroductionByImdburl(String imdburl) {
       String introduction = filmMapper.getFilmIntroductionByImdburl(imdburl);
        return introduction;
    }


    @Override
    public List<Film> getFilmsByCondition(String condition, String keyword) {
        List<Film> films = filmMapper.getFilmsByCondition(condition,keyword);
        return films;
    }


    @Override
    public Film addFilm(Film film) {
        filmMapper.addFilm(film);
        return film;
    }


    @Override
    public int getfilmnum() {
        List<Film> l=filmMapper.getFilms();
        return l.size();
    }

    @Override
    public List<Film> getpageFilms(int pagenum, int pagesize) {
        PageHelper.startPage(pagenum,pagesize);
        List<Film> l=filmMapper.getFilms();
        return l;
    }

    @Override
    public void deletefilm(String id) {
        filmMapper.deletefilm(id);
    }

    @Override
    public List<filmContext> getFilmContext(String filmid) {
        /*int id =Integer.parseInt(filmid);*/
       List list =  filmMapper.getFilmContext(filmid);
        return list;
    }


    @Override
    public filmContext getContextbyid(int filmid) {
        filmContext context=filmMapper.getContextbyid(filmid);
        return context;
    }



    @Override
    public void filmcontextlike_add(String filmcontext_id) {
        filmMapper.filmcontextlike_add( filmcontext_id);
    }



    @Override
    public Film getfilmbyid(String id) {
        Film film =filmMapper.getfilmbyid(id);
        return film;
    }

    @Override
    public void updatefilminfobyid(Film film) {
        filmMapper.updatefilminfobyid(film);
    }

    public void deletefilmcontet_byid(String id ){
        filmMapper.deletefilmcontextbyid(id);
    }

    @Override
    public List getPagefilmContext(int current, int rowCount,String id) {
        PageHelper.startPage(current,rowCount);

        List<filmContext> l = filmMapper.getFilmContext(id);
        return l;
    }


    @Override
    public int getFilmContextnum(String id) {

        List list=filmMapper.getFilmContext(id);
        return list.size();
    }


    /*下面注释掉的数据是长度超过Excel所能接受的长度*/
    @Override
    public InputStream getInputStream() throws Exception{
        String []title =new String[]{"name","directors","writers","actors","tags","areas",
                "languages","releaseDates","duration","otherName","imdbUrl",
                "star","evaluateNum","url"};
        String []title2 = new String[]{"电影名称","导演","编剧","演员","类别","地区","语言","上映时间",
                "时长","别名","imdb地址","评分","评价数","豆瓣地址"};
        List<Film> flist=filmMapper.getFilms();
        List<Object[]> dataList= new ArrayList();
        for (int i=0;i<flist.size();i++){
            Object[] obj=new Object[14];
            obj[0]=flist.get(i).getName();
            obj[1]=flist.get(i).getDirectors();
            obj[2]=flist.get(i).getWriters();
            obj[3]=flist.get(i).getName();
            obj[4]=flist.get(i).getTags();
            obj[5]=flist.get(i).getAreas();
            obj[6]=flist.get(i).getLanguages();
            obj[7]=flist.get(i).getReleaseDates();
            obj[8]=flist.get(i).getDuration();
            obj[9]=flist.get(i).getName();//otherName
            obj[10]=flist.get(i).getName();//imdburl不能用
            obj[11]=flist.get(i).getStar();
            obj[12]=flist.get(i).getEvaluateNum();
            obj[13]=flist.get(i).getUrl();
            /*obj[1]=flist.get(i).getDirectors();
            obj[2]=flist.get(i).getWriters();
            obj[3]=flist.get(i).getName();
            obj[4]=flist.get(i).getTags();
            obj[5]=flist.get(i).getAreas();
            obj[6]=flist.get(i).getLanguages();
            obj[7]=flist.get(i).getReleaseDates();
            obj[8]=flist.get(i).getDuration();
            obj[9]=flist.get(i).getOtherNames();
            obj[10]=flist.get(i).getImdbUrl();
            obj[11]=flist.get(i).getStar();
            obj[12]=flist.get(i).getEvaluateNum();
            obj[13]=flist.get(i).getUrl();*/
            dataList.add(obj);
        }
        WriteExcel exce=new WriteExcel(title2,dataList);
        InputStream in;
        in=exce.export();
        return in;
    }

    @Override
    public InputStream getFilmContentInputStream(String filmid) throws Exception {
        String[] tilte=new String[]{"电影名称","用户","获赞数"};
        List<filmContext> list = filmMapper.getFilmContext(filmid);
        List<Object[]> datalist =new ArrayList();
        for(int i=0;i<list.size();i++){
            Object[] obj=new Object[3];
            obj[0]=list.get(i).getFilmname();
            obj[1]=list.get(i).getUsername();
           // obj[2]=list.get(i).getContext();
            obj[2]=list.get(i).getLikenum();
            datalist.add(obj);
        }
        WriteExcel excel=new WriteExcel(tilte,datalist);
        InputStream in;
        in=excel.export();
        return in;
    }

    @Override
    public void addFilmContext(filmContext context) {
        filmMapper.addFilmContext(context);
    }

}
